package com.ruoyi.web.controller.gcgl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.gcgl.domain.GcFk;
import com.ruoyi.gcgl.domain.GcFklog;
import com.ruoyi.gcgl.service.IGcFkService;
import com.ruoyi.gcgl.service.IGcFklogService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 付款台账Controller
 * 
 * @author ruoyi
 * @date 2023-06-02
 */
@Controller
@RequestMapping("/gcgl/fklog")
public class GcFklogController extends BaseController
{
    private String prefix = "gcgl/fklog";

    @Autowired
    private IGcFklogService gcFklogService;
    @Autowired
    private IGcFkService gcFkService;

    @RequiresPermissions("gcgl:fklog:view")
    @GetMapping()
    public String fklog()
    {
        return prefix + "/fklog";
    }

    /**
     * 查询付款台账列表
     */
    @RequiresPermissions("gcgl:fklog:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(GcFklog gcFklog)
    {
        startPage();
        gcFklog.setDeleteFlag(1);
        List<GcFklog> list = gcFklogService.selectGcFklogList(gcFklog);
        return getDataTable(list);
    }

    /**
     * 导出付款台账列表
     */
    @RequiresPermissions("gcgl:fklog:export")
    @Log(title = "付款台账", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(GcFklog gcFklog)
    {
        List<GcFklog> list = gcFklogService.selectGcFklogList(gcFklog);
        ExcelUtil<GcFklog> util = new ExcelUtil<GcFklog>(GcFklog.class);
        return util.exportExcel(list, "付款台账数据");
    }

    /**
     * 新增付款台账
     */
    @GetMapping("/add/{id}")
    public String add(@PathVariable("id") Long id, ModelMap mmap){
    	GcFk fk = gcFkService.selectGcFkById(id);
    	mmap.put("gcProjectid", fk.getGcProjectid());
    	mmap.put("projectId", fk.getProjectId());
    	mmap.put("fkId", fk.getId());
    	mmap.put("projectNo", fk.getProjectNo());
    	mmap.put("xmmc", fk.getXmmc());
    	mmap.put("ckdw", fk.getCkdw());
    	mmap.put("xmtype", fk.getXmtype());
        return prefix + "/add";
    }

    /**
     * 新增保存付款台账
     */
    @RequiresPermissions("gcgl:fklog:add")
    @Log(title = "付款台账", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(GcFklog gcFklog)
    {
    	SimpleDateFormat sdy = new SimpleDateFormat("yyyy");
		SimpleDateFormat sdm = new SimpleDateFormat("MM");
		int year = Integer.parseInt(sdy.format(new Date()));
		int month = Integer.parseInt(sdm.format(new Date()));
		GcFklog newObj = new GcFklog();
		newObj.setDeleteFlag(1);
		newObj.setCreateAt(DateUtils.getFisrtDayOfMonth(year, 1));
		newObj.setUpdateAt(DateUtils.getLastDayOfMonth(year, 12));
		List<GcFklog> lisCount = gcFklogService.selectGcFklogList(gcFklog);
		gcFklog.setOrderNo(year+"-"+(lisCount.size()+1));
		gcFklog.setDeleteFlag(1);
		gcFklog.setCreateUser(getLoginName());
		gcFklog.setCreateAt(new Date());
		gcFklog.setUpdateUser(getLoginName());
		gcFklog.setUpdateAt(new Date());
		return toAjax(gcFklogService.insertGcFklog(gcFklog));
    }

    /**
     * 修改付款台账
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        GcFklog gcFklog = gcFklogService.selectGcFklogById(id);
        mmap.put("gcFklog", gcFklog);
        return prefix + "/edit";
    }

    /**
     * 修改保存付款台账
     */
    @Log(title = "付款台账", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(GcFklog gcFklog)
    {
		gcFklog.setUpdateUser(getLoginName());
		gcFklog.setUpdateAt(new Date());
        return toAjax(gcFklogService.updateGcFklog(gcFklog));
    }

    /**
     * 删除付款台账
     */
    @RequiresPermissions("gcgl:fklog:remove")
    @Log(title = "付款台账", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(gcFklogService.deleteGcFklogByIds(ids));
    }
}
